import { defineStore } from 'pinia'
import { Names } from './store.name'
import {getApiList} from '../server'
import type { RootObject, Children, ChinaAdd, ChinaTotal, LocalCityNCOVDataList } from '../utils/js/type'
type User = {
  name:string,
  age:number
}

// let result:User = {
// name:'飞机',
// age:999
// }

const Login = (): Promise<User> =>{
  return new Promise((resolve)=>{
    setTimeout(()=>{
      resolve({
        name: '小飞机',
        age: 999
      })
    },2000)
  })
}

export const useTestStore = defineStore(Names.TEST, {
  state: () => {
    return {
      cureent: 100,
      name: 'fuheng',
      user: <User>{},
      list: <RootObject>{},
      yqItem: <Children[]>[],
      chinaAdd: <ChinaAdd>{},
      chinaTotal: <ChinaTotal>{},
      cityDetail: <LocalCityNCOVDataList[]>[]
    }
  },
  //computed 修饰一些值
  getters: { 
    newName():string{
    return  `$-${this.name}-${this.getUserName}`
    },
    getUserName():number{
    return this.user.age
    }
  },
  //methods 可以做同步 异步都可以做 提交state
  actions: {
      // setCurrent(num:number){
      // this.cureent = num
      // }
    async getList(){
      const result = await getApiList()
      this.list = result
      this.chinaAdd = this.list.diseaseh5Shelf.chinaAdd
      this.chinaTotal = this.list.diseaseh5Shelf.chinaTotal
      this.cityDetail = this.list.localCityNCOVDataList.slice(0,10)
    },
   async setUse(){
      const result = await Login()
       this.user = result
      this.setNanme('111')
    },
    setNanme(name:string){
      this.name = name
    }

  }
})

